export const goodsAttr = {

    data () {
        return {
            skuList:[],
            selectProductAttr:[],
            columnCheck:[],
        }
    },
    created() {
    },
    methods: {
        
        getAttrTemplateList(){
            this.$api.getMAttrTemplateList({ pageSize:10000 }).then(response => {
                this.attrTemplateList = response.data.content
            }).catch((res) => {
                this.$message({  message: '模板列表获取失败', type: 'error' })
            })
        },

        // 选择已有模板
        chooseAttrTemplate( index ){
            let  template = this.attrTemplateList[index];
            console.log( template );
            this.selectProductAttr = JSON.parse( template.attrJsonStr );
            // 默认未选择
            this.selectProductAttr.forEach(element => {
                console.log( this.selectProductAttr );
                element.values=[];
                // element.values=  element.itemList ;
            });
            this.$forceUpdate();
        },

        changeSkuType(){
            if( this.dataForm.skuType == 0  ){
                var rand = Math.floor(Math.random () * 900) + 100
                var tmp = Date.parse( new Date() ).toString().substr(0,10);
                this.skuList = [];
                this.columnCheck = [];
                this.skuList.push({
                    attrInfo: [],
                    skuPrice:  '',
                    skuCostprice:'',
                    skuSaleprice: '',
                    skuStock: '',
                    skuIntegral: '',
                    skuImgurl: '',
                    skuCode: tmp+"-"+ rand,
                }) 
            }else if( this.dataForm.skuType == 1 ) {

                this.skuList = [];
            }
        },

        //移除一个标签
        handleClose(tag,idx) {
            let temp =this.selectProductAttr[idx].itemList;
            temp.splice(temp.indexOf(tag), 1);
        },

        //选项点击事件
        handleClick(tag,idx) {
            let temp2 =this.selectProductAttr[idx].values;
            if( temp2.indexOf(tag) > -1){
                temp2.splice(temp2.indexOf(tag), 1);
            }else{
                temp2.push(tag);
            }
            this.$forceUpdate();
        },

        //展示输入框
        showInput(idx) {
            for (let index = 0; index < this.selectProductAttr.length; index++) {
                const element = this.selectProductAttr[index];
                element.inputFlag= ( index == idx );
            }
            let _this=this
            this.$nextTick(_ => {
                this.$forceUpdate();
                    setTimeout(function()  {
                        _this.$refs.sssInput[0].$refs.input.focus();
                        _this.$forceUpdate();
                }, 0);
            });
        },

        //新增输入框
        handleInputConfirm(index) {

            let temp =this.selectProductAttr[index].itemList;
            let newValue = this.inputValue;
            
            this.selectProductAttr[index].inputFlag= false;
            this.inputValue = '';
            if(temp.length == 10){
                this.$message({message: '至多添加10个选项', type: 'error'})
                return false;
            }else if(temp.indexOf(newValue) > -1){
                this.$message({message: '该选项已经存在', type: 'error'})
                return false;
            }else if(newValue ==''){
                this.$message({message: '选项值不能为空', type: 'error'})
            }else{
                temp.push( newValue ) ;
            }
        },
        
        //销售属性列渲染
        getProductSkuSp(row, index) {
            if( row.attrInfo.length > 0 ){
                let valueName = row.attrInfo[index].value;
                return valueName
            }else{
                return ""
            }
        },

        //刷新渲染SKU列表 
        handleRefreshProductSkuList() {
            // 勾选了规格，才调用方法
            let skuArray = []//所选属性值
            this.columnCheck = [];
            // 将选中的规格组合成一个大数组 [[1, 2], [a, b]...]
            this.selectProductAttr.forEach(element => {

                if (element.values.length > 0) {
                    // 动态列设置
                    this.columnCheck.push({   attrName: element.attrName})
                    skuArray.push(element.values)
                }
            })
            if (skuArray.length == 0 || skuArray.length < this.selectProductAttr.length  ) {
                this.$message.error('每种规格请至少选择一个销售属性')
                return false;
            } 
            this.$confirm('刷新列表将删除之前的sku数据，是否要刷新', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {

                //刷新之前先清空sku列表
                this.skuList = [];
                //生成SKU列表
                this.getSkuData([], 0, skuArray)
            }).catch(() => {
              this.$message.error('sku组合失败')
            })
        },
        

        //生成SKU信息
        getSkuData(skuArr = [], i, list) {
            for (let j = 0; j < list[i].length; j++) {
                let name = this.selectProductAttr[i].attrName;
                if (i < list.length - 1) {
                    skuArr[i] = { name:name , value : list[i][j]  } 
                    this.getSkuData(skuArr, i + 1, list) // 递归循环
                } else {
                    let temp = { name:name , value : list[i][j]  } ;
                    var tmp = Date.parse( new Date() ).toString().substr(0,10);
                    var rand = Math.floor(Math.random () * 900) + 100
                    // 渲染SKU列表
                    this.skuList.push({
                        attrInfo: [...skuArr, temp ],
                        skuPrice:  '',
                        skuCostprice:'',
                        skuSaleprice: '',
                        skuStock: '',
                        skuImgurl: '',
                        skuCode: tmp+"-"+ rand,
                    }) 
                }
            }
        },

        // 移除某一行SKU 信息
        handleRemoveProductSku(index, row) {
            const list = this.skuList
            if (list.length === 1) {
                list.pop()
            } else {
                list.splice(index, 1)
            }
        },
        

        //根据商品id获取 SKU列表
        getSkuListByGoodsId(goodsId){
            // 获取sku信息
            this.$api.getSkuInfoByGoodsId({goodsId: goodsId}).then(response => {
                response.data.forEach(item => {
                    item.attrInfo = JSON.parse(item.attrInfo)    //列表SKU信息
                })
                this.skuList = response.data
            }).catch(() => {
                this.$message({message: '商品SKU列表获取失败', type: 'error'})
            })
        },
        
        
        //根据商品id获取 SKU列表
        getSkuListByNearbyGoodsId(goodsId){
            // 获取sku信息
            this.$api.getSkuListByNearbyGoodsId({goodsId: goodsId}).then(response => {
                response.data.forEach(item => {
                    item.attrInfo = JSON.parse(item.attrInfo)    //列表SKU信息
                })
                this.skuList = response.data
            }).catch(() => {
                this.$message({message: '附近商品商品SKU列表获取失败', type: 'error'})
            })
        },
        




    }
}









 